package zhai.docking.alipay.util;

import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import zhai.docking.alipay.constant.StringConstant;

import java.io.File;
import java.io.InputStream;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.util
 * @project-name zhai-docking-alipay
 * @description 文件工具类
 * @create-datetime 2021/1/22 13:30
 */
public class FileUtil {

    public static final String UNIT_B = "B";
    public static final String UNIT_KB = "KB";
    public static final String UNIT_MB = "MB";
    public static final String UNIT_GB = "GB";
    public static final String UNIT_TB = "TB";

    public static final Long ONE_KB = 1024L;
    public static final Long ONE_MB = 1048576L;
    public static final Long ONE_GB = 1073741824L;
    public static final Long ONE_TB = 1099511627776L;

    public static final String FILE_TYPE_JPG = "jpg";
    public static final String FILE_TYPE_JPEG = "jpeg";
    public static final String FILE_TYPE_BMP = "bmp";
    public static final String FILE_TYPE_PNG = "png";
    public static final String FILE_TYPE_GIF = "gif";


    /***
     * 根据文件名获取文件类型
     * @return
     */
    public static String getFileType(String fileName) {
        return fileName.substring(fileName.lastIndexOf(StringConstant.POINT) + 1);
    }

    public static Boolean isFileType(String fileType, String... fileTypes) {
        return StringUtil.equalsAny(fileType, fileTypes);
    }

    /**
     * 判断文件大小上限
     * @param len
     * @param size
     * @param unit
     * @return
     */
    public static boolean checkMaxSize(Long len, int size, String unit) {
        double fileSize = 0;
        if (UNIT_B.equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if (UNIT_KB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_KB;
        } else if (UNIT_MB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_MB;
        } else if (UNIT_GB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_GB;
        } else if (UNIT_TB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_TB;
        }
        if (fileSize > size) {
            return false;
        }
        return true;
    }

    /**
     * 判断文件大小下限
     * @param len
     * @param size
     * @param unit
     * @return
     */
    public static boolean checkMinSize(Long len, int size, String unit) {
        double fileSize = 0;
        if (UNIT_B.equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if (UNIT_KB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_KB;
        } else if (UNIT_MB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_MB;
        } else if (UNIT_GB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_GB;
        } else if (UNIT_TB.equals(unit.toUpperCase())) {
            fileSize = (double) len / ONE_TB;
        }
        if (fileSize < size) {
            return false;
        }
        return true;
    }

    /**
     * 获取文件名
     * @param file
     * @return
     */
    public static String getFileName(MultipartFile file) {
        return file.getOriginalFilename();
    }

    /**
     * InputStream 转 File
     */
    @SneakyThrows
    public static void copyInputStreamToFile(InputStream source, File target) {
        FileUtils.copyInputStreamToFile(source, target);
    }

}